package demo.practice.leetcode_eveyday;

import java.util.ArrayList;
import java.util.List;

public class P1893 {

//    class UF{
//
//        int range[][];//范围
//        int parent[];
//
//
//        public  UF
//
//    }

    public static void main(String[] args) {
        P1893 p1893 = new P1893();
        p1893.isCovered(new int[][]{{37, 49}, {5, 17}, {8, 32}}, 29, 49);
    }

    class KV {
        public int k;
        public int v;

        public KV(int k, int v) {
            this.k = k;
            this.v = v;
        }

        public int getK() {
            return k;
        }

        public int getV() {
            return v;
        }
    }

    public boolean isCovered(int[][] ranges, int left, int right) {

        if (ranges == null || ranges.length == 0 || left > right) {
            return false;
        }
        List<KV> list = new ArrayList<>();
        for (int[] range : ranges) {

            list.add(new KV(range[0], range[1]));
        }


        list.sort((o1, o2) -> o1.getK() == o2.getK() ? o1.getV() - o2.getV() : o1.getK() - o2.getK());
        List<KV> cleaned = new ArrayList<>();

        //little
        int l = list.get(0).getK();
        //big
        int b = list.get(0).getV();
        for (int i = 1; i < list.size(); i++) {
            KV next = list.get(i);
            if (next.getK() - b > 1) {
                //新建区间
                cleaned.add(new KV(l, b));

                l = list.get(i).getK();
                b = list.get(i).getV();
            } else {
                b = Math.max(b, list.get(i).getV());
            }
        }

        cleaned.add(new KV(l, b));
        for (KV kv : cleaned) {

            if (kv.getK() <= left && kv.getV() >= right) {
                return true;
            }
        }

        return false;
    }
    /**
     * 社会也不是淘汰35岁的人，淘汰的只是那种35岁拿着高薪干不赢刚毕业的人。有本事就去创业，没本事就好好上班，没必要天天抱怨，那样就真的一点儿本事都没有了
     */
}
